package com.qiyun.asyncarrange;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.concurrent.*;

//@RunWith(SpringRunner.class)
public class CompletableFutureTest01 {
    public static ThreadPoolExecutor executor = new ThreadPoolExecutor(5,
            200,
            10,
            TimeUnit.SECONDS,
            new LinkedBlockingDeque<>(100000),
            Executors.defaultThreadFactory(),//默认线程工厂
            new ThreadPoolExecutor.AbortPolicy()//拒绝策略
            //如果不想抛弃还要执行。可以使用new ThreadPoolExecutor.CallerRunsPolicy()
    );

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        /**
         * CompletableFuture 提供了4个静态方法来创建一个异步操作
         * static CompletableFuture<void> runAsync(Runnable runnable)
         * public static CompletableFuture<void> runAsync(Runnable runnable,Executor executor)
         * public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier)
         * public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier, Executor executor)
         */

        /**
         * 01.runAsync
         */
        System.out.println("main--start---");
        // 无返回值的
       /* CompletableFuture.runAsync(() -> {
            System.out.println("当前线程：" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("运行结果...");
        }, executor);*/



        /**
         * 02.supplyAsync
         */
        // 有返回值的
        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
            System.out.println("当前线程：" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("运行结果..." + i);
            return i;

        },executor);
        System.out.println("异步执行的结果为"+future.get());
        System.out.println("main--end---");

    }


}
